package com.changgou.consume.service.impl;

import com.changgou.consume.dao.SeckillGoodsMapper;
import com.changgou.consume.dao.SeckillOrderMapper;
import com.changgou.consume.service.SeckillOrderService;
import com.changgou.seckill.pojo.SeckillOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: HuangFu
 * @Date: 2020/2/24 15:35
 * @Description:
 */
@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createOrder(SeckillOrder seckillOrder) {

        // 插入订单
        int result = seckillOrderMapper.insertSelective(seckillOrder);
        if(result <=0){
            return 0;
        }
        //更新商品库存
        result = seckillGoodsMapper.updateStockCount(seckillOrder.getSeckillId(),1);
        if(result <=0){
            return 0;
        }
        return 1;
    }
}
